import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.base.Joiner;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.google.common.io.Files;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.*;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.atomic.AtomicInteger;

public class FetchDXMTask  implements Runnable{
    static Set<String> fileSet = new HashSet<String>();
    static AtomicInteger atomicInteger = new AtomicInteger(0);
    CloseableHttpClient closeableHttpClient = HttpClientBuilder.create().build();
    LinkedBlockingQueue<String> idpaths;
    public FetchDXMTask(LinkedBlockingQueue queue){
        idpaths=queue;
    }

    /**
     * 抓取  每个目录对应的   product_type_files
     * @param idPath
     * @throws IOException
     * @throws SQLException
     */
    public void fetch(String idPath,String site) throws IOException, SQLException {
        String[] strs = idPath.split(",");
        strs[0]=null;
        String idpathstr = Joiner.on("/").skipNulls().join(strs);
        Integer i=atomicInteger.incrementAndGet();
        HttpGet httpGet = new HttpGet("http://rs.beta.dianxiaomi.com/amazon/browse-tree/"+site+"/"+idpathstr+".json?v=5.6");
        addHeader(httpGet);
        HttpResponse httpResponse = closeableHttpClient.execute(httpGet);
        String content = EntityUtils.toString(httpResponse.getEntity());
        JSONArray jsonArray=null;
        try{
            JSONObject jsonObject =JSON.parseObject(content);
            jsonArray = jsonObject.getJSONArray("ProductTypeFile");
            List<String> strings = jsonArray.toJavaList(String.class);
            fileSet.addAll(strings);
            String files = Joiner.on(",").join(strings);
            Connection connection = JdbcUtil.getConnection();
            PreparedStatement pstmt = connection.prepareStatement("update   amazon_catagory set product_type_files=? WHERE id_path=?");
            pstmt.setString(1,files);
            pstmt.setString(2,idPath);
            pstmt.execute();
            JdbcUtil.releaseConn(connection);
            System.out.println("抓取第"+i+"个  idpath = "+idpathstr +"成功");
        }catch (Exception e){
            System.out.println(content);
            System.out.println("抓取第"+i+"个  idpath = "+idpathstr +"失败");
            e.printStackTrace();
        }


    }

    public static void addHeader(HttpGet httpPost){
        //httpPost.addHeader("Cookie","Hm_lvt_f8001a3f3d9bf5923f780580eb550c0b=1488357494,1488467232,1488771733,1489053137; Hm_lpvt_f8001a3f3d9bf5923f780580eb550c0b=1489376211");
        httpPost.addHeader("User-Agent","Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36");
        httpPost.addHeader("Accept","*/*");
        httpPost.addHeader("Accept-Language","zh-CN,zh;q=0.8,en;q=0.6");
    }

    public void run() {
        String idpath;
        try {
            while((idpath=idpaths.take())!=null){
                String[] a = idpath.split(";");
                fetch(a[0],a[1]);
            }
        } catch (Exception e){
            e.printStackTrace();
        }
    }
}
